import { defineStore } from "pinia";
import {
  listBorrowPage, //借调单分页查询
  DeleteBorrow, //删除借调单
} from "@/utils/api/InventoryManagement/IMborrow/IMborrow";
import { dictlist } from "@/utils/api/dict/dict"; //字典列表
import { useMemberStore } from "@/stores/modules/member";

const tableHeader = [
  "借调单号",
  "借调类型",
  "借调时间",
  "借调状态",
  "操作人",
  "审批状态",
  "操作",
];
export const useIMborrowStore = defineStore(
  "IMborrow",
  () => {
    // 定义表头
    const IMborrow_tableHeader = tableHeader;
    //借调单分页列表
    let Borrowlist: any = [];
    let IMborrow_list = reactive({ Borrowlist });
    //总条数
    let totalCount: any = ref(0);
    // 借调状态列表
    let StatusList: any = [];
    let borrowStatusList: any = reactive({ StatusList });
    // 借调类型列表
    let TypeList: any = [];
    let borrowTypeList: any = reactive({ TypeList });

    /******************** 公共方法 ***********************/
    //获取借调单分页列表
    const getListBorrowPage = async (data: any) => {
      const userStore: any = useMemberStore();
      // 调用接口获取数据
      const res = await listBorrowPage({
        pageNum: data.pageNum || 1,
        pageSize: data.pageSize || 10,
        searchParam: data.searchParam || "",
        StartDate: data.StartDate || "",
        EndDate: data.EndDate || "",
        BorrowStatus: data.BorrowStatus || "",
        BorrowType: data.BorrowType || "",
        GodownCode: userStore.GodownCode || "",
        GodownKeeper: userStore.user_id,
      });
      let resdata = res?.data;
      resdata?.list.forEach((item: any) => {
        item.checked = false;
      });
      IMborrow_list.Borrowlist = resdata?.list;
      totalCount.value = resdata?.totalCount;
      return resdata;
    };
    //获取借调状态列表
    const getDictList = async () => {
      const res = await dictlist({
        DictStatus: "Normal",
        DictType: "BorrowStatus",
      });
      borrowStatusList.StatusList = res.data;
      return res.data;
    };
    //获取借调类型列表
    const getBorrowTypeList = async () => {
      const res = await dictlist({
        DictStatus: "Normal",
        DictType: "BorrowType",
      });
      borrowTypeList.TypeList = res.data;
      return res.data;
    };
    //删除借调单
    const deleteBorrow = async (data: any) => {
      const res = await DeleteBorrow(data);
      return res;
    };

    /*********************** 对外暴露 ********************************/
    return {
      IMborrow_tableHeader,
      getListBorrowPage,
      IMborrow_list,
      getDictList,
      getBorrowTypeList,
      borrowStatusList,
      borrowTypeList,
      totalCount,
      deleteBorrow,
    };
  },
  /**********************************TODO: 持久化***********************/
  {
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
        getItem(key) {
          return uni.getStorageSync(key);
        },
      },
      paths: ["IMborrow_tableHeader", ""],
    },
  }
);
